<html>
<head><meta charset="utf-8"><title>Blanket trait impls lookup · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html">Blanket trait impls lookup</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222987736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Blanket%20trait%20impls%20lookup/near/222987736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html#222987736">(Jan 16 2021 at 15:33)</a>:</h4>
<p>I'm considering alternatives to the way we look up traits for associated items.<br>
Right now, we store every assoc item in <code>fst</code> and then search it, it works for blanket impls too, but it feels pretty indirect in general and it gets tricky when there's no input at all and we still want to find every applicable assoc item for any given struct.</p>
<p>As an alternative, it feels like we can locate all trait impls for a certain <code>hir::Type</code>, similar to the way the lens code works.<br>
But as you can see it's currently less precise: we're unable to detect any blanket impls (see how the lens show a single impl even though there are actually 2 of them):</p>
<p><a href="/user_uploads/4715/eDkeR_YSSXwqnORNOOzYb5ru/image.png">image.png</a> </p>
<div class="message_inline_image"><a href="/user_uploads/4715/eDkeR_YSSXwqnORNOOzYb5ru/image.png" title="image.png"><img src="/user_uploads/4715/eDkeR_YSSXwqnORNOOzYb5ru/image.png"></a></div><p>Does anybody have an idea, if it's hard to find those blanket impls?<br>
Maybe an idea on which way is better in general for solving the "find the trait for an assoc item" task?<br>
cc <span class="user-mention" data-user-id="129457">@Florian Diebold</span> as a chalk wizard.</p>



<a name="222988721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Blanket%20trait%20impls%20lookup/near/222988721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html#222988721">(Jan 16 2021 at 15:52)</a>:</h4>
<p>I actually just wrote a comment about that on your PR. In the end, you can't get around asking Chalk whether your type implements each of some set of traits. You can use such heuristics to maybe make that set smaller though. You'd basically have to have an index from <code>TyFingerprint</code> to traits, plus a list of traits that have blanket impls (<em>or can have built-in impls</em>). And then you'd have to try each one of those.</p>



<a name="222988945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Blanket%20trait%20impls%20lookup/near/222988945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html#222988945">(Jan 16 2021 at 15:57)</a>:</h4>
<p>Yes, just read that one.</p>
<p>Ok, I'd better finish the fst endeavours first then, thank you a lot.</p>



<a name="222989350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Blanket%20trait%20impls%20lookup/near/222989350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html#222989350">(Jan 16 2021 at 16:04)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Blanket.20trait.20impls.20lookup/near/222988721">said</a>:</p>
<blockquote>
<p>I actually just wrote a comment about that on your PR. In the end, you can't get around asking Chalk whether your type implements each of some set of traits. You can use such heuristics to maybe make that set smaller though. You'd basically have to have an index from <code>TyFingerprint</code> to traits, plus a list of traits that have blanket impls (<em>or can have built-in impls</em>). And then you'd have to try each one of those.</p>
</blockquote>
<p>hmm - is this because the Chalk API requires it? or because there's no way even in theory to look up impls by type?</p>



<a name="222989374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Blanket%20trait%20impls%20lookup/near/222989374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html#222989374">(Jan 16 2021 at 16:04)</a>:</h4>
<p>(context: rustdoc is really slow looking up impls: <a href="#narrow/stream/247081-t-compiler.2Fperformance/topic/rustdoc.20calls.20.60for_each_relevant_impl.60.20a.20lot">https://rust-lang.zulipchat.com/#narrow/stream/247081-t-compiler.2Fperformance/topic/rustdoc.20calls.20.60for_each_relevant_impl.60.20a.20lot</a>)</p>



<a name="222990145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Blanket%20trait%20impls%20lookup/near/222990145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html#222990145">(Jan 16 2021 at 16:19)</a>:</h4>
<p>keep in mind that you can't really ask e.g. "does <code>Vec</code> implement <code>Into</code>". <code>Vec</code> isn't a type, <code>Vec&lt;T&gt;</code> for some <code>T</code> is, and it can't implement <code>Into</code>, only <code>Into&lt;T&gt;</code> for some (set of) <code>T</code>. so in the case of autocompletion, you're really asking "does this type implement <code>Into&lt;T&gt;</code> for any <code>T</code>", and for rustdoc I assume the question would be "is there any <code>T</code> and <code>U</code> such that <code>Vec&lt;T&gt;</code> implements <code>Into&lt;U&gt;</code>". And the answer to that question is basically determined by a logic program in a turing-complete language, and it's a different logic program for each trait. So, there might be ways to optimize the process of evaluating all those programs in this situation, but in the end I don't think you can't get around doing so</p>



<a name="222990209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Blanket%20trait%20impls%20lookup/near/222990209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html#222990209">(Jan 16 2021 at 16:20)</a>:</h4>
<p>that makes sense, thanks</p>



<a name="222990315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Blanket%20trait%20impls%20lookup/near/222990315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Blanket.20trait.20impls.20lookup.html#222990315">(Jan 16 2021 at 16:22)</a>:</h4>
<p>(also, the ones you throw away by making an index of impls for each struct would be the ones that are fastest to evaluate anyway, so this might not actually help a huge amount)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>